package com.software.modelcenter.service;

import com.software.modelcenter.entity.Collection;
import com.software.modelcenter.entity.ModelInfo;
import com.software.modelcenter.repository.CollectionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Transactional
@Service
public class CollectionService {

    @Autowired
    private CollectionRepository collectionRepository;


    public Collection creatCollection(Collection collection) {
        collection.setCollectionId(UUID.randomUUID().toString());
        Date now = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String collectionTime = format.format(now);
        collection.setCollectionTime(collectionTime);
        Collection save = collectionRepository.save(collection);
        return save;
    }


    public Collection findCollection(String userId, String modelId) {
        Collection find = collectionRepository.findCollectionByUserIdAndModelId(userId, modelId);
        return find;
    }


    public void deleteCollection(Collection find) {
        collectionRepository.delete(find);
    }

    public List<Collection> findAllByUserId(String userId) {
        return collectionRepository.findAllByUserId(userId);
    }


}
